package ai.people.platform.archives.service.impl;

import ai.people.core.nebula.entity.NebulaGraph;
import ai.people.core.nebula.factory.NebulaSessionFactoryBean;
import ai.people.core.nebula.request.GraphPathRequest;
import ai.people.core.nebula.util.NebulaQueryUtils;
import ai.people.platform.archives.service.GraphAdminService;
import com.vesoft.nebula.client.graph.net.SessionWrapper;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.Set;

/**
 * @author zhenglin
 * @date 2022/4/12
 */
@Service
@Slf4j
@RequiredArgsConstructor
public class GraphAdminServiceImpl implements GraphAdminService {
    /**
     * nebula会话工厂bean
     */
    private final NebulaSessionFactoryBean nebulaSessionFactoryBean;

    @Deprecated
    public NebulaGraph path(GraphPathRequest request) throws Exception {
        SessionWrapper session = nebulaSessionFactoryBean.getObject();
        return NebulaQueryUtils.path(session, request);
    }

    /**
     * 路径
     *
     * @param vid 从视频
     * @return {@link NebulaGraph}
     * @throws Exception 异常
     */
    @Override
    public NebulaGraph path(String vid, String direction) throws Exception {
        SessionWrapper session = nebulaSessionFactoryBean.getObject();
        LinkedHashSet<NebulaGraph.NebulaPath> nebulaPaths = new LinkedHashSet<>();
        Set<String> nodes = new HashSet<>();
        NebulaQueryUtils.recursionGetPath(session, vid, nebulaPaths, nodes, direction);
        NebulaGraph result = new NebulaGraph();
        result.setPaths(nebulaPaths);
        if (CollectionUtils.isNotEmpty(nebulaPaths)) {
            NebulaGraph.NebulaPath next = nebulaPaths.iterator().next();
            NebulaGraph.NebulaNode rootNode = next.getFrom();
            result.setRootNode(rootNode);
        } else {
            NebulaGraph.NebulaNode rootNode = NebulaQueryUtils.getVertexByVid(session, vid);
            result.setRootNode(rootNode);
        }
        result.setNodesVidSet(nodes);

        return result;
    }

    @Override
    public NebulaGraph path(String vid, String direction, Collection collection) throws Exception {
        SessionWrapper session = nebulaSessionFactoryBean.getObject();
        LinkedHashSet<NebulaGraph.NebulaPath> nebulaPaths = new LinkedHashSet<>();
        Set<String> nodes = new HashSet<>();
        NebulaQueryUtils.recursionGetPath(session, vid, nebulaPaths, nodes, direction,collection);
        NebulaGraph result = new NebulaGraph();
        result.setPaths(nebulaPaths);
        if (CollectionUtils.isNotEmpty(nebulaPaths)) {
            NebulaGraph.NebulaPath next = nebulaPaths.iterator().next();
            NebulaGraph.NebulaNode rootNode = next.getFrom();
            result.setRootNode(rootNode);
        } else {
            NebulaGraph.NebulaNode rootNode = NebulaQueryUtils.getVertexByVid(session, vid);
            result.setRootNode(rootNode);
        }
        result.setNodesVidSet(nodes);

        return result;
    }

}
